﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Data.SqlClient;
using System.Data.Sql;
namespace ERP
{
    public partial class frmSettings : DevExpress.XtraEditors.XtraForm
    {
        private string ConfigFileName =Application.StartupPath + @"\config.ini";
        /// <summary>
        /// Required designer variable.
        /// </summary>
        public frmSettings()
        {
            // Required for Windows Form Designer support
            InitializeComponent();

            // TODO: Add any constructor code after InitializeComponent call
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            if (File.Exists(ConfigFileName))
            {
                DataSet ds = new DataSet("Config");
                ds.ReadXml(ConfigFileName);
                if (ds != null)
                {
                    this.txtServer.Text = ds.Tables[0].Rows[0]["SERVER"].ToString();
                    this.txtDatabase.Text = ds.Tables[0].Rows[0]["DATABASE"].ToString();
                    this.txtUsername.Text = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                    this.txtPassword.Text = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool ok = true;
            SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString = GetConfig();
            try
            {
                dataConnection.Open();                
            }
            catch
            {
                ok = false;
                XtraMessageBox.Show(this,"Kết Nối Thất Bại. Không thể lưu.", "Thông Báo");
            }
            if (ok == false) return;
            DataTable dt = new DataTable();
            dt.TableName = "CONFIG";
            dt.Columns.Add("SERVER", typeof(string));
            dt.Columns.Add("DATABASE", typeof(string));
            dt.Columns.Add("USERNAME", typeof(string));
            dt.Columns.Add("PASSWORD", typeof(string));
            dt.Columns.Add("OTHER", typeof(string));
            DataRow r = dt.NewRow();
            r["SERVER"] = this.txtServer.Text.Trim();
            r["DATABASE"] = this.txtDatabase.Text.Trim();
            r["USERNAME"] = this.txtUsername.Text.Trim();
            r["PASSWORD"] = this.txtPassword.Text.Trim();
            r["OTHER"] = "Integrated Security=True";
            dt.Rows.Add(r);            
            dt.WriteXml(ConfigFileName);
            XtraMessageBox.Show("Cập nhật thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.DialogResult = DialogResult.OK;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString = GetConfig();
            try
            {
                dataConnection.Open();
                XtraMessageBox.Show("Kết nối thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                XtraMessageBox.Show("Kết nối thất bại !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.Cursor = Cursors.Default;
        }

        private string GetConfig()
        {
            string ret = "";
            ret = "Data Source=" + this.txtServer.Text.Trim();
            ret = ret + ";Initial Catalog=" + this.txtDatabase.Text.Trim();
            if (this.txtUsername.Text.Trim() != "")
                ret = ret + ";User ID=" + this.txtUsername.Text.Trim();
            if (this.txtPassword.Text.Trim() != "")
                ret = ret + ";Password=" + this.txtPassword.Text.Trim();          
            return ret;
        }

        
    }
}